package coint.btc.controller.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import coint.btc.dto.JueseDTO;
import coint.btc.service.auth.JueseService;
import coint.btc.util.HttpStatus;
import coint.btc.util.WriteJson;
import coint.btc.validator.MergeGroups;
import coint.btc.validator.SaveGroups;
import coint.btc.vo.JueseVo;
import coint.btc.vo.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;



@Api(value="角色管理controller",tags={"角色管理操作接口"})
@RestController
@RequestMapping("/console/juese")
public class JueseController {

	@Autowired
	public JueseService  jueseService;

	/***
	 * 
	 * 
	 * @Title: addUser
	 * 
	 * @Description: 添加角色
	 * 
	 * @author wyx
	 * 
	 * @date 2018年7月12日
	 * 
	 * @param juese
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation(httpMethod = "POST", value = "添加角色")
	public ModelMap addUser(@Validated(value = { SaveGroups.class }) JueseDTO  juese , BindingResult result) {
		
		if(result.hasErrors()) {
			return WriteJson.errorWebDataMsg(HttpStatus.NOT_PARAMETER, "添加失败", result.getFieldError().getDefaultMessage());
		}

		jueseService.addJuese(juese);

		return WriteJson.successData("添加成功");

	}

	/***
	 * 
	 * 
	 * @Title: delete
	 * 
	 * @Description:通过角色id删除角色
	 * 
	 * @author wyx
	 * 
	 * @date 2018年7月12日
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping("/delete/{id}")
	@ApiOperation(httpMethod = "GET", value = "删除角色")
	public ModelMap delete(@PathVariable(name = "id", required = true) Integer id) {
		jueseService.deleteJueseByid(id);
		return WriteJson.successData("添加成功");

	}
	
	
	
	
	/***
	 * 
	
	 * @Title: update 
	
	 * @Description:修改角色信息
	 
	 * @author wyx  
	
	 * @date 2018年7月13日  
	
	 * @param juese
	 * @return
	 */
	@PostMapping("/update")
	@ApiOperation(httpMethod = "POST", value = "修改角色")
	public ModelMap update(@Validated(value = { MergeGroups.class }) JueseDTO juese) {

		int flag = jueseService.updateJuese(juese);
		if (flag <= 0) {
			return WriteJson.successData("修改失败");
		}

		return WriteJson.successData("修改成功");

	}

	/***
	 * 
	
	 * @Title: findone 
	
	 * @Description:查询角色详情
	 
	 * @author wyx  
	
	 * @date 2018年7月13日  
	
	 * @param id
	 * @return
	 */
	@GetMapping("/find/{id}")
	@ApiOperation(httpMethod = "GET", value = "添加角色")
	public ModelMap findone(@PathVariable(name = "id", required = true) Integer id) {
		JueseVo juese = jueseService.findJueseInfoVoById(id);
		return WriteJson.successDataMsg("查询成功", juese);

	}
	
	
	/***
	 * 
	
	 * @Title: findone 
	
	 * @Description:分页查询角色
	 
	 * @author wyx  
	
	 * @date 2018年7月13日  
	
	 * @param id
	 * @return
	 */
	@PostMapping("/juese/page")
	@ApiOperation(httpMethod = "POST", value = "角色列表")
	public ModelMap findJuesePage(JueseDTO parms) {
		Page<JueseVo> list  = new Page<JueseVo>();
		list.setPage(parms.getPage());
		list.setRows(parms.getRows());
		list= jueseService.findJuesePageByParms(list, parms);
		return WriteJson.successDataMsg("查询成功", list);
	}
	
	
	
	

}
